﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Kiiro.Common.Entities;
using Kiiro.Common.Interfaces;
using Kiiro.Common.Utility;
using Kiiro.SiteDefinition.ControlTemplates;
using Kiiro.Model;
using Kiiro.SiteDefinition.Core;
using Kiiro.SiteDefinition.MasterPage;

namespace Kiiro.SiteDefinition.ProjectPages
{
    public partial class NewTeamMember : BasePage
    {
        #region Properties
        public TeamMember TeamMember
        {
            get;
            set;
        }

        public CustomFieldCollection CustomFields
        {
            get { return ( CustomFieldCollection ) v2CustomFieldCollection; }
        }

        public NavigationSub NavigationSub
        {
            get { return KiiroMaster.NavigationSub; }
        }

        public FormButtons Buttons
        {
            get { return ( FormButtons ) v2FormButtons; }
        }
        #endregion

        #region Page Events
        protected override void OnInit ( EventArgs e )
        {
            base.OnInit ( e );

            CustomFields.ControlMode = SPControlMode.New;
            Buttons.SaveButton.ControlMode = SPControlMode.New;
            Buttons.CancelButton.ControlMode = SPControlMode.New;
        }

        protected void Page_Load ( object sender, EventArgs e )
        {
            CustomFields.EntityType = typeof ( TeamMember );
            CustomFields.Title = "Additional Information";

            SPContext context = CustomModel.GetCustomFieldsContext ( typeof ( TeamMember ), ItemId, Context, SiteCollectionId, ServerRelativeUrl );
            context.FormContext.FormMode = SPControlMode.New;

            Buttons.SaveButton.ItemContext = context;
            Buttons.SaveButton.RenderContext = context;

            Buttons.CancelButton.ItemContext = context;
            Buttons.CancelButton.RenderContext = context;
            Buttons.CancelButton.RedirectUrl = Cancel ( );

            litTitle.Text = "New Team Member";

            if ( KiiroMaster.V2Header.Project != null && !string.IsNullOrEmpty ( KiiroMaster.V2Header.Project.Title ) )
            {
                litTitle.Text = KiiroMaster.V2Header.Project.Title + " - New Team Member";
            }

            if ( PermissionBitsTeams [ 0 ] )
            {
                LoadUsersDropDown ( );
            }
            else
            {
                Response.Redirect ( string.Format ( "{0}/Team.aspx", ServerRelativeUrl ) );
            }
        }
        #endregion

        #region Methods
        internal override string Cancel ( )
        {
            return string.Format ( "{0}/Team.aspx", ServerRelativeUrl );
        }

        internal override bool Save ( )
        {
            TeamMember = new TeamMember ( );

            if ( PermissionBitsTeams [ 0 ] )
            {
                TeamMember.CreatedBy = CurrentUser;
                TeamMember.CreatedDate = DateTime.Now;
                TeamMember.IsProjectManager = chkIsProjectManager.Checked;
                TeamMember.RoleOnProject = txtRoleOnProject.Text;
                TeamMember.ServerRelativeUrl = ServerRelativeUrl;
                TeamMember.SiteCollectionId = SiteCollectionId;
                TeamMember.ModifiedBy = CurrentUser;
                TeamMember.ModifiedDate = DateTime.Now;
                TeamMember.CustomFieldNamesAndValues = CustomFields.HarvestCustomFieldValues ( );

                if ( !string.IsNullOrEmpty ( ddlUsers.SelectedValue ) )
                {
                    TeamMember.User = UserModel.GetOrCreateUser ( ddlUsers.SelectedValue, SiteCollectionId, ServerRelativeUrl );
                    TeamMember.UserId = TeamMember.User.Id;
                }

                TeamMember.EditType = EditType.Created;

                TeamMember = TeamModel.Save ( TeamMember, SiteCollectionId, ServerRelativeUrl );

                if ( TeamMember.IsValid ( ) )
                    Response.Redirect ( Cancel ( ) );
                else
                    PopulateErrorMessages ( );
            }
            else
            {
                Response.Redirect ( Cancel ( ) );
            }
            return true;
        }

        private void LoadUsersDropDown ( )
        {
            if ( !IsPostBack )
            {
                List<User> users = UserModel.GetUsersNotOnTeam ( SiteCollectionId, ServerRelativeUrl );

                ddlUsers.Items.Add ( new ListItem ( "Select A User →", "" ) );

                foreach ( User user in users )
                {
                    string login = user.IsFormsUser ? string.Format ( "kiiromembership:{0}", user.Login ) : user.Login;
                    ddlUsers.Items.Add ( new ListItem ( user.FullName, login ) );
                }

                ddlUsers.SelectedIndex = 0;
            }
        }

        private void PopulateErrorMessages ( )
        {
            if ( TeamMember.UserIdMessage.Count > 0 )
            {
                lblDDLUsersMessage.Text = "Valid user must be selected.";
                lblDDLUsersMessage.Visible = !string.IsNullOrEmpty ( lblDDLUsersMessage.Text );

            }
            lblRoleOnProjectMessage.Text = FormattingHelpers.GetErrorMessageText ( TeamMember.RoleOnProjectMessage );
            lblRoleOnProjectMessage.Visible = !string.IsNullOrEmpty ( lblRoleOnProjectMessage.Text );
        }
        #endregion
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.SiteDefinition/12/TEMPLATE/FEATURES/Kiiro.ProjectPages/Team/NewTeamMember.aspx.cs $
 * 
 * 14    2/08/10 2:28p Camerons
 * 
 * 13    1/11/10 11:51a Camerons
 * 
 * 12    1/06/10 12:39p Camerons
 * 
 * 11    15/10/09 12:17p Camerons
 * Fix GetOrCreateUser (login, ...)
 * 
 * 10    5/10/09 11:07a Camerons
 * RE: Create user on demand, checks for HasUnlimitedUser before creating
 * 
 * 9     1/10/09 3:28p Camerons
 * Load Expanded ADGroups into drop down on NewTeamMember. Create user on
 * demand if selected user doesn't exist.
 * 
 * 8     30/09/09 4:08p Camerons
 * Update all links that point to UserPages, pass User.SPUserId as the
 * ItemId so that we can safely run GetOrCreateUser
 * 
 * 7     17/08/09 11:07a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/